<?php
declare(strict_types=1);

namespace App\Logic;

use App\Event\AdministratorLogsEvent;
use App\Models\Account as AccountModel;

class Account
{
    public function getRoleIdByAccountId(int $accountId)
    {
        return AccountModel::where('id', $accountId)->pluck('role_id')->toArray()[0];
    }

    public function count(array $where = [])
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where['account'])) $map[] = ['account', 'like', '%'.$where['account_name'].'%'];
        if (!empty($where['account_name'])) $map[] = ['account_name', 'like', '%'.$where['account_name'].'%'];
        if (!empty($where['role_id'])) $map[] = ['role_id', '=', $where['role_id']];
        return AccountModel::where($map)->count();
    }

    public function list(int $start, int $limit, array $where = [])
    {
        $map[] = ['delete_time', '=', 0];
        if (!empty($where['account'])) $map[] = ['account', 'like', '%'.$where['account_name'].'%'];
        if (!empty($where['account_name'])) $map[] = ['account_name', 'like', '%'.$where['account_name'].'%'];
        if (!empty($where['role_id'])) $map[] = ['role_id', '=', $where['role_id']];
        return AccountModel::with('role')->where($map)->orderBy('id','desc')->offset($start)->limit($limit)->get();
    }

    public function add(array $data)
    {
        $model = new AccountModel();
        $model->role_id = $data['role_id'];
        $model->account_name = $data['account_name'];
        $model->account_email = $data['account_email'];
        $model->account_phone = $data['account_phone'];
        $model->account = $data['account'];
        $model->password = $data['password'];
        $model->account_status = $data['account_status'];
        if ($model->save($data)) {
            event(new AdministratorLogsEvent("添加后台账号"));
            return true;
        }
        return false;
    }

    public function update(int $id, array $data)
    {
        $model = AccountModel::find($id);
        $model->role_id = $data['role_id'];
        $model->account_name = $data['account_name'];
        $model->account_email = $data['account_email'];
        $model->account_phone = $data['account_phone'];
        $model->account_status = $data['account_status'];
        if ($model->save($data)){
            event(new AdministratorLogsEvent("修改后台账号信息"));
            return true;
        }
        return false;
    }

    public function modify(int $id, array $data)
    {
        $model = AccountModel::find($id);
        $model->password = $data['password'];
        if ($model->save($data)){
            event(new AdministratorLogsEvent("修改后台账号密码"));
            return true;
        }
        return false;
    }

    public function status(int $id)
    {
        $model = AccountModel::find($id);
        if ($model->account_status == 1) {
            $model->account_status = 2;
        } else {
            $model->account_status = 1;
        }
        if ($model->save()){
            event(new AdministratorLogsEvent("修改后台账号状态"));
            return true;
        }
        return false;
    }

    public function delete(int $id)
    {
        $model = AccountModel::find($id);
        if ($model->delete()){
            event(new AdministratorLogsEvent("删除后台账号"));
            return true;
        }
        return false;
    }

    public function find(int $id)
    {
        return AccountModel::find($id);
    }

    public function has($id)
    {
        $model = AccountModel::find($id);
        if (empty($model)) return false;
        return true;
    }


    public function countByRoleId(int $roleId)
    {
        return AccountModel::where('role_id', $roleId)->count();
    }

    public function checkAccount(string $account)
    {
        $model = AccountModel::where('account',$account)->first();
        if (empty($model)) return true;
        return false;
    }
}
